define([], function (require, exports) {
    exports.ready = function (hashCode, data, cite) {
        // 注册该功能要用到的数据字典
        yufp.lookup.reg('DATA_STS,SYS_TYPE,ANNOUNCEMENT_STATUS');
        // 创建virtual model
        yufp.custom.vue({
            el: '#announcementManagement',
            // 以m_开头的属性为UI数据不作为业务数据，否则为业务数据
            data: function () {
                var me = this;
                return {
                    funcDataUrl:backend.msmFrontWebService + '/api/AnnouncementManagement/getAllNotice',
                    fileName:'',
                    uploadAction: '/api/AnnouncementManagement/fileUpload',
                    data:'',
                    headers:{
                        'Authorization': 'Bearer ' + yufp.service.getToken(),
                    },
                    // 上传的文件列表
                    fileList: [],
                    boardName: '',
                    boardContent: '',
                    createTime:'',
                    boardPublisher:'',
                    datePick: {// 限制不可选择之前的日期
                        disabledDate: function (time) {
                            return time.getTime() < Date.now() - 8.64e7;
                        }
                    },
                    tableData7: [],
                    dialogFormVisible: false,
                    dialogType: '',
                    fw: null,
                    dialogDisabled: true,
                    activeName: ['1', '2'], // 默认显示name为1的
                    detail: "",ormDisabled: false,
                    inputDisabled:false,
                    modFormButtons: [
                        {
                            label: "保存",
                            type: 'primary',
                            hidden: false,
                            op: 'submit',
                            click: function (model) {
                                me.saveEditFunc();
                                me.dialogFormVisible = false;
                            }
                        },
                        {
                            label: "取消",
                            hidden: false,
                            click: function (model) {
                                me.restInputValue();
                            }
                        }],
                    modFields: [{
                        columnCount: 1,
                        fields:
                            [{
                                field: 'boardName',
                                placeholder: "公告名称",
                                label: "公告名称",
                            }
                            ],
                    }, {
                        columnCount: 1,
                        fields: [{
                            field: 'boardContent',
                            placeholder: "公告内容",
                            label: "公告内容",
                            type: 'textarea',
                            size:'large',
                        }]
                    },],
                };
            },
            computed: {
                action: function () {
                    // 获取全路径
                    var me = this;
                    return yufp.service.getUrl({url: me.uploadAction});
                }
            },
            // 钩子
            mounted: function () {},
            methods: {
                searchFn: function () {
                    var me = this;
                    var param = {
                        condition: JSON.stringify({
                            boardName: me.$refs.queryForm.formdata.boardName,
                            boardStatus: me.$refs.queryForm.formdata.boardStatus,
                            userId: yufp.session.userId,
                        })
                    };
                    me.$refs.announcementTable.remoteData(param);
                },
                handleExceed :function(files, fileList) {
                    this.$message.warning(`当前限制选择 1 个文件，本次选择了 ${files.length} 个文件，共选择了 ${files.length + fileList.length} 个文件`);
                },
                /** 文件移除时 */
                handleRemove: function (file, fileList) {
                    //访问接口将其从本地删除，并且将绑定值也置空，避免更新时直接将地址存入。
                    yufp.logger.info(file, fileList);
                },
                // 文件改变时
                fileChange:function(file, fileList) {
                    yufp.logger.info(file, fileList);
                    this.fileList = fileList
                },
                // 文件移除之前
                beforeRemove:function(file, fileList) {
                    return this.$confirm(`确定移除 ${file.name}？`);
                },
                onSuccess: function (response, file, fileList) {
                    var me =this;
                    me.fileName=response;
                },
                onError:function (){
                    this.$message.error('系统错误，请联系管理员');
                },
                // 上传文件
                handleUpload: function () {

                },
                changeInputDisabled:function (type){
                    if (type==='detail'){
                        return false
                    }
                    else {
                        return true
                    }
                },
                saveEditFunc: function () {
                    var _this = this;
                    console.log(_this.fileName)
                    _this.dialogFormVisible = false;
                    let param = {
                        condition: JSON.stringify({
                            boardName: _this.$refs.queryForm.formdata.boardName,
                            boardStatus: _this.$refs.queryForm.formdata.boardStatus,
                            userId: yufp.session.userId,
                            //
                        })
                    };
                    yufp.service.request({
                        url: backend.msmFrontWebService + '/api/AnnouncementManagement/editNotice',
                        method: 'get',
                        data: {
                            condition: JSON.stringify({
                                //通过绑定值赋值给后端数据库存储
                                boardLoad:_this.fileName ===''? '': _this.fileName,
                                // file:_this.fileList[0] ? _this.fileList[0].name :"",
                                userId: yufp.session.userId,
                                orgId: yufp.session.orgId,
                                boardId:_this.dialogType === 'edit' ?_this.$refs.announcementTable.selections[0].id :'',
                                boardName:_this.$refs.modForm.formModel.boardName,
                                boardContent: _this.$refs.modForm.formModel.boardContent,
                                boardPublisher:yufp.session.userName
                            })
                        },
                        callback: function (code, message, response) {
                            _this.restInputValue();
                            if (response === 1) {
                                _this.$refs.announcementTable.remoteData(param);
                                console.info("成功");
                                //获取上级网格名称和ID
                            } else {
                                console.info("失败");
                            }
                        }
                    });
                },
                checkFileList:function (){
                  var me = this;
                  this.$message({message: '请选择至少一个选项', type: 'warning'});
                },
                deleteFn:function (){
                    //删除时的选项校验
                    if (this.$refs.announcementTable.selections.length === 0) {
                        this.$message({message: '请选择至少一个选项', type: 'warning'});
                        return;
                    }
                    var _this = this;
                    let param = {
                        condition: JSON.stringify({
                            boardName: _this.$refs.queryForm.formdata.boardName,
                            boardStatus: _this.$refs.queryForm.formdata.boardStatus,
                            userId: yufp.session.userId,
                            //
                        })
                    };
                    var boardIdArray =[];
                    for (var i=0;i<_this.$refs.announcementTable.selections.length;i++){
                        boardIdArray.push(_this.$refs.announcementTable.selections[i].id)
                    }
                    yufp.service.request({
                        url: backend.msmFrontWebService + '/api/AnnouncementManagement/deleteNotice',
                        method: 'get',
                        data: {
                            condition: JSON.stringify({
                                userId: yufp.session.userId,
                                orgId: yufp.session.orgId,
                                boardId:boardIdArray,
                            })
                        },
                        callback: function (code, message, response) {
                            if (response !== 0) {
                                _this.$refs.announcementTable.remoteData(param);
                                console.info("成功");
                                //获取上级网格名称和ID
                            } else {
                                console.info("失败");
                            }
                        }
                    });
                },
                //每次执行显示操作，新增操作执行清空，修改执行查询，
                restInputValue:function (){
                    var _this = this;
                    _this.dialogFormVisible = false;
                    //因初次加载的时候读取不到modForm所以判断是否存在并作出对应操作
                    _this.$refs.modForm? _this.$refs.modForm.formModel.boardName = '':'';
                    _this.$refs.modForm?_this.$refs.modForm.formModel.boardContent = '':'';
                    _this.createTime= '';
                    _this.boardPublisher= '';
                    _this.fileList= [];
                },
                select: function (row, index) {
                    // this.$refs.announcementTable.toggleRowSelection(row, true);
                    if (row.withdrawState == 2)//这个判断根据你的情况而定
                        return false;//不可勾选
                    else
                        return true;//可勾选
                },
                addFn:function(){
                    var me = this;
                    me.inputDisabled =false;
                    //进行新增的时候无需确认是否选择
                    me.restInputValue();
                    this.opType('add', true);
                },
                downloadFn:function (row){
                    console.log(row.boardLoad)},
                dateFormat: function(row, column) {
                    if (row.createTime){
                        let dt = new Date(row.createTime)
                        return dt.getFullYear()+'-'+(dt.getMonth()+1)+'-'+ dt.getDate()
                    }
                    else {
                        return null
                    }
                },
                beforeUpload:function (file){
                    const isLt2M = file.size / 1024 / 1024 < 100;
                    if (!isLt2M) {
                        this.$message.error('上传文件大小不能超过 100MB!');
                    }
                    return isLt2M;
                },
                publishFn:function (){
                    var me = this;
                    let param = {
                        condition: JSON.stringify({
                            boardName: me.$refs.queryForm.formdata.boardName,
                            boardStatus: me.$refs.queryForm.formdata.boardStatus,
                            userId: yufp.session.userId,
                            //
                        })
                    };
                    var boardIdArray =[];
                    for (var i=0;i<me.$refs.announcementTable.selections.length;i++){
                        console.log(me.$refs.announcementTable.selections[i])
                        if(me.$refs.announcementTable.selections[i].boardStatus==='1'){
                            this.$message({message: '所选选项中有已发布内容请重新选择', type: 'warning'});
                            return;
                        }
                        boardIdArray.push(me.$refs.announcementTable.selections[i].id)
                    }
                    yufp.service.request({
                        url: backend.msmFrontWebService + '/api/AnnouncementManagement/publishNotice',
                        method: 'get',
                        data: {
                            condition: JSON.stringify({
                                boardId:boardIdArray,
                                userId: yufp.session.userId,
                                orgId: yufp.session.orgId,
                            })
                        },
                        callback: function (code, message, response) {
                            if (response !== 0) {
                                me.$refs.announcementTable.remoteData(param);
                                console.info("成功");
                                //获取上级网格名称和ID
                            } else {
                                console.info("失败");
                            }
                        }
                    });
                },
                lookupInformation: function (type) {
                    var me = this;
                    if (this.$refs.announcementTable.selections.length !== 1) {
                        this.$message({message: '请选择一个选项', type: 'warning'});
                        return;
                    }
                    me.opType(type, true);
                    yufp.service.request({
                        url: backend.msmFrontWebService + '/api/AnnouncementManagement/getOnlyNotice',
                        method: 'get',
                        data: {
                            condition: JSON.stringify({
                                boardId:me.$refs.announcementTable.selections[0].id,
                                userId: yufp.session.userId,
                                orgId: yufp.session.orgId,
                            })
                        },
                        callback: function (code, message, response) {
                            var createTime = response.data[0].createTime
                            console.log(response.data[0].boardLoadName)
                            if (code === 0 && response.code === 0) {
                                //获取上级网格名称和ID
                                // me.fileList=response.data[0].boardLoadName;
                                me.fileList=[{name: response.data[0].boardLoadName, url: response.data[0].boardLoad}];
                                me.$refs.modForm.formModel.boardName= response.data[0].boardName;
                                me.$refs.modForm.formModel.boardContent= response.data[0].boardContent;
                                me.createTime= createTime;
                                me.boardPublisher= response.data[0].boardPublisher;
                            } else {
                                console.info("失败");
                            }
                        }
                    });
                },
                opType: function (type, disabled) {
                    this.dialogFormVisible = true;
                    this.dialogType = type;
                    this.formDisabled = disabled;
                },
            }
        });
    };

    // 消息处理
    exports.onmessage = function (type, message) {
    };

    // page销毁时触发destroy方法
    exports.destroy = function (id, cite) {
    };
});
